package cn.yd.rb.controller;

import cn.yd.rb.po.*;
import cn.yd.rb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    public AdminService adminService;

    @Autowired
    private UserService userService;

    @Autowired
    private NovelService novelService;

    @Autowired
    private NewsService newsService;

    @Autowired
    private ChapterService chapterService;

    @RequestMapping("/login")
    public String login(Model model, HttpSession session, Admin admin) throws  Exception{

        try{
            Admin adminRes =adminService.findAdminByLogin(admin);
            session.setAttribute("admin", adminRes);
        }catch(Exception e){
            model.addAttribute("mess", "用户名或密码错误！");
            return "/admin/admin_login";
        }
        return "redirect:/admin/index";
    }
    @RequestMapping("/index")
    public String index(Model model) throws  Exception {
        return  "/admin/index";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session) throws  Exception {
        session.removeAttribute("admin");
        return "/admin/admin_login";
    }
    @RequestMapping("/getUserCountsAndNovelCountsAndNewsCounts")
    public String getUserCounts(Model model){
        Integer userCounts=userService.findUserCounts();
        model.addAttribute("userCounts",userCounts);

        Integer novelCounts=novelService.findNovelCounts();
        model.addAttribute("novelCounts",novelCounts);

        Integer newsCounts=newsService.findNewsAll().size();
        model.addAttribute("newsCounts",newsCounts);
        return "admin/home";
    }

    @RequestMapping("/getAllNovels")
    public String getAllNovels(Model model, @RequestParam(value = "pc",required = true,defaultValue = "1") Integer pc, NovelQueryVo novelQueryVo){
        Integer pageSiez=7;

        PageBean<Novel> pageBean=novelService.findNovelAll(pc,pageSiez,novelQueryVo);
        model.addAttribute("pageBean",pageBean);

        return "admin/goods";
    }

    @RequestMapping("/logicDeleteNovel/{arr}")
    public String logicDeleteNovel(@PathVariable("arr")List<Integer> ids){
        novelService.logicDeleteNovel(ids);
        return "redirect:/admin/getAllNovels";
    }

    @RequestMapping("/getAllDeltedNovels")
    public String getAllDeltedNovels(Model model,@RequestParam(value = "pc",required = true,defaultValue = "1")Integer pc,NovelQueryVo novelQueryVo){
        Integer pageSiez=7;

        PageBean<Novel> pageBean=novelService.findDeletedNovelAll(pc,pageSiez,novelQueryVo);
        model.addAttribute("pageBean",pageBean);

        return "admin/deletedNovel";
    }
    @RequestMapping("/recoverNovel/{arr}")
    public String recoverNovel(@PathVariable("arr")List<Integer> arr){
        novelService.recoverNovel(arr);
        return "redirect:/admin/getAllDeltedNovels";
    }

    @RequestMapping("/addNewNovel")
    public String addNewNovel(Novel novel, MultipartFile file, BindingResult bindingResult) throws Exception {
            //文件上传
            if(file.getSize()!=0){
                /* nginx图片服务器
                 * */
                //实例化工具类，得到其对象
                FastDFSClient Client = new FastDFSClient("D:\\client.conf");
                //直接上传
                String filePath = Client.uploadFile(file.getBytes(),"jpg");
                System.out.println(novel.getIsFree());

                //将图片地址更新到商品表的pic中
                novel.setPic(filePath);
                novelService.addNewNovel(novel);}

        return "redirect:/admin/getAllNovels";//返回商品列表方法--重定向
    }
    //总txt上传
    @RequestMapping("/addNovelTxt/{id}")
    public String addNovelTxt(MultipartFile file,@PathVariable("id")Integer id) throws Exception {

        if(file.getSize()!=0){
            FastDFSClient client=new FastDFSClient("D:\\client.conf");
            String filePath=client.uploadFile(file.getBytes(),"zip");
            Novel novel=novelService.findNovelById(id);
            novel.setTexturl(filePath);
            novelService.updateNovelTxt(novel);
        }
        return "redirect:/admin/getAllNovels";
    }
    @RequestMapping("/getAllNews")
    public String getAllNews(Model model, @RequestParam(value = "pc",required = true,defaultValue = "1")Integer pc, NewQueryVo newQueryVo){
        Integer pageSiez=7;

        PageBean<News> pageBean=newsService.findNovelAll(pc,pageSiez,newQueryVo);
        model.addAttribute("pageBean",pageBean);

        return "admin/news";
    }

    @RequestMapping("/addNews")
    public String addNews(MultipartFile file,News news) throws Exception {
        if(file.getSize()!=0){
            FastDFSClient client=new FastDFSClient("D:\\client.conf");
            String newsUrl=client.uploadFile(file.getBytes(),"txt");
            news.setLinkurl(newsUrl);
            newsService.addNews(news);
        }
        return "redirect:/admin/getAllNews";
    }
    //分章阅读
    @RequestMapping("/addTxtByChapter/{id1}")
    public String addTxtByChapter(MultipartFile file,@PathVariable("id1")Integer id1,Chapter chapter) throws Exception {
        if(file.getSize()!=0){
            FastDFSClient client =new FastDFSClient("D:\\client.conf");
            String chapterpath=client.uploadFile(file.getBytes(),"txt");
            chapter.setChapterurl(chapterpath);
            chapter.setNovelId(id1);
            chapterService.addChapter(chapter);
        }
        return "redirect:/admin/getAllChapter/"+id1;
    }
    @RequestMapping("/getAllChapter/{id}")
    public String getAllChapter(HttpServletRequest request, @PathVariable("id")Integer id, Model model, @RequestParam(value = "pc",required = true,defaultValue = "1")Integer pc, ChapterQueryVo chapterQueryVo){
        Integer pageSiez=7;

        HttpSession session=request.getSession();
        session.setAttribute("novelid",id);


        PageBean<Chapter> pageBean=chapterService.findAllChapterForNovel(1,session,pc,pageSiez,chapterQueryVo,id);
        model.addAttribute("pageBean",pageBean);



        return "admin/novelChapterDetail";
    }
}

